package cn.fanqinghong.basic.util;

import cn.fanqinghong.basic.jwt.*;
import cn.fanqinghong.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

/**
 * FAN
 * 2022/6/25  11:49
 */
@Slf4j
public class LoginContext {
    /**
     * 获取登陆人的信息
     * @param request
     * @return
     */
    public static Logininfo getLoginUser(HttpServletRequest request){
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            return null;}
        //获取公钥
//        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("hrm_auth_rsa.pub").getFile());
        PublicKey publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
        //通过公钥解析token，登陆载荷体
        Payload<LoginResult> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginResult.class);
        LoginResult loginResult = payload.getUserInfo();
        return loginResult.getLogininfo();
    }
}
